package com.ckx.config.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        String requestId = "";
        Object requestIdObj = RequestContextHolder.getRequestAttributes().getAttribute("requestId", RequestAttributes.SCOPE_REQUEST);
        if (requestId != null) {
            requestId = requestIdObj.toString();
        }
        MDC.put("requestId", requestId);
        template.header("requestId", requestId);
        template.header("version", "gray");
    }

}
